package com.equis.estacionamientos.bean.marker {
	import com.equis.estacionamientos.bean.estacionamiento.FullEstacionamiento;
	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.MapEvent;
	import com.google.maps.PaneId;
	import com.google.maps.interfaces.IInfoWindow;
	import com.google.maps.interfaces.IMap;
	import com.google.maps.interfaces.IMarker;
	import com.google.maps.interfaces.IPane;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.overlays.OverlayBase;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	[Bindable]
	[RemoteClass(alias="ar.com.mindset.mapalimpieza.marker.MapaMarker")]
	public class MapaMarker extends OverlayBase implements IMarker {
	    private var _latLon:LatLng;
	    private var _comp:MarkerComponent;
	    private var _options:MarkerOptions;
	    private var _denunciaId:ObjectId;
	    private var _latitude:Number;
	    private var _longitude:Number;
	    private var _tipo:String;
	    private var _fullEstacionamiento:FullEstacionamiento;
	
	
	    public function MapaMarker() {
	        super();
	        addEventListener(MapEvent.OVERLAY_ADDED, handleMapEvent, false, 0, true);
	        addEventListener(MapEvent.OVERLAY_REMOVED, handleMapEvent, false, 0, true);
	    }
		
		public function set color(value:String):void {
			_color = value;
		}
		
		public function get color():String {
			return _color;
		}
		
	    public function set denunciaId(value:ObjectId):void {
	        _denunciaId = value;
	    }
	
	    public function get denunciaId():ObjectId {
	        return _denunciaId;
	    }
	
	    public function set latitude(value:Number):void {
	        _latitude = value;
	    }
	
	    public function get latitude():Number {
	        return _latitude;
	    }
	
	    public function set longitude(value:Number):void {
	        _longitude = value;
	    }
	
	    public function get longitude():Number {
	        return _longitude;
	    }
	
	    public function set tipo(value:String):void {
	        _tipo = value;
	        if (_tipo != "") {
	            var c:MarkerComponent = new MarkerComponent();
	            c.markerData = this;
	            _comp = c;
	            //_comp = ApplicationLocator.instance.getMarkerGraphic(value);
	        }
	    }
	
	    public function get tipo():String {
	        return _tipo;
	    }
	
	
	    public function isHidden():Boolean {
	        return false;
	    }
	
	    public function hide():void {
	    }
	
	    public function show():void {
	    }
	
	
	    public function openInfoWindow(opts:InfoWindowOptions = null, useSeparatePane:Boolean = false):IInfoWindow {
	        return pane.map.openInfoWindow(getLatLng(), opts);
	    }
	
	    public function closeInfoWindow():void {
		//	pane.map.removeEventListener(MapEvent.INFOWINDOW_CLOSED, viewMarker);
	        pane.map.closeInfoWindow();
	    }
	
	
	    public function setOptions(arg:MarkerOptions):void {
	        _options = arg;
	    }
	
	    public function getOptions():MarkerOptions {
	        return _options;
	    }
	
	    override public function getDefaultPane(map:IMap):IPane {
	        return map.getPaneManager().getPaneById(PaneId.PANE_MARKER);
	    }
		
	    override public function positionOverlay(zoom:Boolean):void {
	        var p:Point = pane.fromLatLngToPaneCoords(getLatLng(), true);
	        _comp.x = p.x - _comp.width / 2;
	        _comp.y = p.y - _comp.height;
	    }
	
	    public function getLatLng():LatLng {
	        if (_latLon == null) {
	            _latLon = new LatLng(latitude, longitude);
	        }
	        return _latLon;
	    }
	
	    public function setLatLng(value:LatLng):void {
	        _latLon = value;
	    }
	
	    public function handleMapEvent(event:Event):void {
	        switch (event.type) {
	            case MouseEvent.CLICK:
	                viewInfoWindow();
	                break;
	            case MapEvent.OVERLAY_ADDED:
	                addChild(_comp);
	                break;
	
	            case MapEvent.OVERLAY_REMOVED:
	                removeChild(_comp);
	                break;
	        }
	    }
	
	
	    public function get fullDenunciaDTO():FullDenunciaDTO {
	        if (_fullDenunciaDTO == null) {
	            callService(function (value:FullDenunciaDTO):void {
	                _fullDenunciaDTO = value;
	            });
	        }
	        return _fullDenunciaDTO;
	    }
	
	    private function viewInfoWindow():void {
	        callService(function (value:FullDenunciaDTO):void {
	            loadInfo(value);
	        });
	    }
	
	    private function callService(cbFunction:*):void {
	        var service:MapaDenunciaService = MapaDenunciaServiceFactory.INSTANCE.buildService();
	        service.blocking = true;
	        service.callbackFunction = cbFunction;
	        service.getFullDenunciaDTO(denunciaId);
	    }
	
	    public function loadInfo(denuncia:FullDenunciaDTO):void {
	        var infoWindowOptions:InfoWindowOptions = new InfoWindowOptions();
	        var info:InfoWindowComponent = new InfoWindowComponent();
	        info.addEventListener(InfoWindowComponent.CLOSE_INFO_WINDOW, closeInfoWindowHandler);
	        info.denuncia = denuncia;
	        info.cacheAsBitmap = true;
			info.color = color;
	        infoWindowOptions.customContent = info;
	        infoWindowOptions.padding = 5;
	        infoWindowOptions.hasShadow = false;
	        infoWindowOptions.customOffset = new Point(194, 251);
	        openInfoWindow(infoWindowOptions);
	        _comp.visible = false;
	        _comp.includeInLayout = false;
	    }
	
	    private function closeInfoWindowHandler(event:Event):void {
	        closeInfoWindow();
			_comp.visible = true;
			_comp.includeInLayout = true;

	    }
		
		public function viewMarker(event:Event = null):void {
	        _comp.visible = true;
	        _comp.includeInLayout = true;
		}
		
	}
}
